package com.yohann.designPattern.proxy.jdk;

import java.lang.reflect.Proxy;

/**
 * <p>
 * 代理工厂
 * </p>
 *
 * @author Yohann
 * @since 2021/2/8 16:40
 */
public class ProxyFactory {
    private Object target;

    public ProxyFactory(Object object) {
        this.target = object;
    }

    public Object getProxyInstance() {
        /*
         * newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
         * loader目标对象的类加载器
         * interfaces目标对象的接口类型
         * h 事件处理
         */
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), (proxy, method, args) -> {
            System.out.println("jdk proxy start");
            Object invoke = method.invoke(target, args);
            System.out.println("jdk proxy end");
            return invoke;
        });
    }
}